<?php
	class Advertising extends MY_controller{
		
		protected $table='advertising';
		protected $cate_table='ad_position';
		protected $redis;
		protected $redis_advertising = 'advertising';
		function __construct(){
			error_reporting(E_ALL ^ E_NOTICE); 
			parent::__construct();
		}


		function index($sign = 0,$search = 0,$cid = 0, $limit = 0){
			$offset = 10;
			if($this->input->post()){
				$cid = $this->input->post('cid');
				$search = $this->input->post('search',TRUE)? urldecode($this->input->post('search',TRUE)) : 0;
				$sign = $this->input->post('sign',TRUE)? urldecode($this->input->post('sign',TRUE)) : 0;
				$limit = 0;
			}else{
				$search = urldecode($search);
				$page_config['uri_segment'] = 5; 
			}
			$data = array('sign'=>$sign , 'search' => $search, 'cid' => $cid,  'limit' => $limit);
			$this->load->model('advertisings');
			$this->load->library('pagination');
			$list = $this->advertisings->get_list($limit, $offset, $data);
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['total_rows'] = $list['count'];
			$page_config['per_page'] = $offset;
			$page_config['base_url'] = '/advertising/index/'.$data['sign'].'/'.$data['search'].'/'.$data['cid'];
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$cate_list = $this->advertisings->get_cat_list();
			$this->smarty->assign("cate_list", $cate_list);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("data", $data);
			$this->smarty->assign("list", $list['result']);
			$this->smarty->assign("action", '广告列表');
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("ad/list.html");
		}

		function ad_index($search = 0, $limit = 0){
			$offset = 10;
			if($this->input->post()){
				$search = $this->input->post('search',TRUE)? urldecode($this->input->post('search',TRUE)) : 0;
				$limit = 0;
			}else{
				$search = urldecode($search);
				$page_config['uri_segment'] = 4; 
			}
			$data = array('search' => $search, 'limit' => $limit);
			$this->load->model('advertisings');
			$this->load->library('pagination');
			$list = $this->advertisings->get_cate_list($limit, $offset, $data);
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['total_rows'] = $this->advertisings->get_cate_count($data);
			$page_config['per_page'] = $offset;
			$page_config['base_url'] = '/advertising/ad_index/'.$data['search'];
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("data", $data);
			$this->smarty->assign("list", $list);
			$this->smarty->assign("action", '广告位列表');
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("ad/list_cate.html");
		}

		function edit($id){
			if((int)$id > 0){
				$this->load->model('advertisings');
				$cate_list = $this->advertisings->get_cate_list();
				$info = get_field_by_where($this->table, '*', 'id=' . $id);
				$info['nick'] = '';
				if(($info['ad_type'] == 2 || $info['ad_type'] == 4) && !empty($info['top_id'])) {
					$user = get_field_by_where('user', 'nick', 'id=' . $info['top_id']);
					if(!empty($user['nick'])) {
						$info['nick'] = $user['nick'];
					}
				}
				$this->smarty->assign("action", '编辑广告');
				$this->smarty->assign("info", $info);
				$this->smarty->assign("cate_list", $cate_list);
			}
			$this->smarty->display("ad/save.html");
		}

		function edit_cate($id){
			if((int)$id > 0){
				$this->load->model('advertisings');
				$info = get_field_by_where($this->cate_table, '*', 'id=' . $id);
				if($info['cid'] > 0){
					$cate_info = get_field_by_where('category', 'cat_name', 'id=' . $info['cid']);
					$info['cat_name'] = $cate_info['cat_name'];
				}
				$this->smarty->assign("action", '编辑广告分类');
				$this->smarty->assign("info", $info);
			}
			$this->smarty->assign("cate_list", $cate_list);
			$this->smarty->display("ad/save_cate.html");
		}

		/**
		* 函数功能介绍：添加广告
		* 创建人: 	Huang YongJun
		* 创建时间: 2015-06-11 10:04
		* 最后修改人：
		* 最后修改时间：
		*/
		function add(){
			$this->load->model('advertisings');
			$cate_list = $this->advertisings->get_cat_list();
			
			$this->smarty->assign("cate_list", $cate_list);
			$this->smarty->assign("action", '添加广告');
			$this->smarty->display("ad/save.html");
		}

		function ad_add(){
			$this->load->model('advertisings');
			$cate_list = $this->advertisings->get_cate_list();
			
			$this->smarty->assign("cate_list", $cate_list);
			$this->smarty->assign("action", '添加广告位');
			$this->smarty->display("ad/save_cate.html");
		}

		/**
		* 函数功能介绍：保存数据
		* 创建人: 	Huang YongJun
		* 创建时间: 2015-06-11 10:21
		* 最后修改人：
		* 最后修改时间：
		*/
		function save(){
			if( ! $this->input->post()) {
				show_message('/index', "非法操作！");exit;
			}
			$post_data = $this->input->post();
			$data = $info = array();
			$data['ad_position_sign']	= $post_data['ad_position_sign'];
			$data['ad_type']			= $post_data['ad_type'];
			$data['ad_name'] 			= $post_data['ad_name'];
			$data['ad_url'] 			= $post_data['ad_url'];
			$data['top_id']				= 0;

			if($data['ad_type'] == 2) {//用户
				$data['top_id'] = !empty($post_data['uid']) ? $post_data['uid'] : 0;
				$info = get_field_by_where('user', 'id AS uid,sex,nick,head_image_url,province,city', 'id=' . $data['top_id']);
			}elseif($data['ad_type'] == 4){
				$data['top_id'] = !empty($post_data['anchor_id']) ? $post_data['anchor_id'] : 0;
				$info = get_field_by_where('user', 'id AS uid,sex,nick,head_image_url,province,city', 'id=' . $data['top_id']);
			}else if ($data['ad_type'] == 3) {//视频
				$data['top_id'] = !empty($post_data['vid']) ? $post_data['vid'] : 0;
				$info = get_field_by_where('video AS v LEFT JOIN mi_user AS u ON (v.uid = u.id)', 'v.id AS vid,v.uid,u.sex,u.nick,u.head_image_url', 'v.id=' . $data['top_id']);
			}
			
			if(!empty($post_data['ad_image_url']) && !strstr($post_data['ad_image_url'], 'http://') ){
				$data['ad_image_url'] = get_qiniu_url($post_data['ad_image_url']);
				
			}else {
				$data['ad_image_url'] = $post_data['ad_image_url'];
			}
			$data['sort']			= intval($post_data['sort']);
			$data['status']			= intval($post_data['status']);
			$data['add_time'] 		= !empty($post_data['add_time']) ? $post_data['add_time'] : time();
			
			if(intval($post_data['id']) > 0){
				$data['id'] = intval($post_data['id']);
				$this->update_data($data,$info);
			}else{
				$this->insert_data($data,$info);
			}
		}

		/**
		* 函数功能介绍：添加数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:31
		* 最后修改人：
		* 最后修改时间：
		*/
		function insert_data($data,$info){
			$this->db->insert($this->table, $data);
			$data['id'] = $this->db->insert_id();
			if($data['id'] > 0){
				$data = array_merge_recursive($data, $info);
				/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
				$this->advertising_redis_model->redis_class->save_advertising($data['ad_position_sign'], $data);*/
				show_message($_SERVER['HTTP_REFERER'], "广告添加成功");
				exit;
			}else{
				show_message($_SERVER['HTTP_REFERER'], "广告添加失败，请重试！");
				exit;
			}
		}

		
		/**
		* 函数功能介绍：修改数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:41
		* 最后修改人：
		* 最后修改时间：
		*/
		function update_data($data,$info = array()){
			$this->db->where('id', (int) $data['id']);
 			$result = $this->db->update($this->table, $data);
 			if($result){
				$data = array_merge_recursive($data, $info);

				/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
				$this->advertising_redis_model->redis_class->save_advertising($data['ad_position_sign'], $data);*/

 				show_message($_SERVER['HTTP_REFERER'], "广告修改成功");
 			}else{
				show_message($_SERVER['HTTP_REFERER'], "广告修改失败,请重试！");
 			}
		}

		/**
		* 函数功能介绍：修改广告排序
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:46
		* 最后修改人：
		* 最后修改时间：
		*/
		function update_sorts() {
			$this->load->model('advertisings');
			$sort = $this->input->post('sort');
			if(is_array($sort)){
				$this->load->library('redis',array('model'=>'advertising_redis', 'channel'=>'advertising'), 'advertising_redis_model');
				foreach($sort as $key=>$val) {
					$this->advertisings->update($key, array('sort' => $val));
					$this->advertising_redis_model->redis_class->change_sort($this->input->post('sign'), $key, $val);
				}
			}
			show_message($_SERVER['HTTP_REFERER'], "广告排序修改成功");
		}

		/**
		* 函数功能介绍：保存广告位数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:54
		* 最后修改人：
		* 最后修改时间：
		*/
		function save_cate(){
			if( ! $this->input->post()) {
				show_message('/index', "非法操作！");exit;
			}
			$post_data = $this->input->post();
			$data['ad_position_name'] 		= $post_data['ad_position_name'];
			$data['ad_position_height'] 	= $post_data['ad_position_height'];
			$data['ad_position_width'] 		= $post_data['ad_position_width'];
			if($post_data['sign']){
				$data['sign'] 		= $post_data['sign'];
			}
			$data['project_type']	= !empty($post_data['project_type']) ? $post_data['project_type'] : 0;
			$data['type']			= !empty($post_data['type']) ? $post_data['type'] : 0;
			$data['sort']			= intval($post_data['sort']);
			$data['add_time'] 		= !empty($post_data['add_time']) ? $post_data['add_time'] : time();
			if(intval($post_data['id']) > 0){
				$data['id'] = intval($post_data['id']);
				$data['old_project_type']	= $post_data['old_project_type'];
				$this->update_cate_data($data);
			}else{
				$this->insert_cate_data($data);
			}
		}

		/**
		* 函数功能介绍：添加广告位数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:54
		* 最后修改人：
		* 最后修改时间：
		*/
		function insert_cate_data($data){
			$this->db->insert($this->cate_table, $data);
			$data['id'] = $this->db->insert_id();
			if($data['id'] > 0){
				/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'ad_cate'),'advertising_redis_model');
				$this->advertising_redis_model->redis_class->save_ad_cate($data['sign'], $data);*/
				show_message($_SERVER['HTTP_REFERER'], "广告位添加成功");
				exit;
			}else{
				show_message($_SERVER['HTTP_REFERER'], "广告位添加失败，请重试！");
				exit;
			}
		}

		/**
		* 函数功能介绍：修改广告位数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:54
		* 最后修改人：
		* 最后修改时间：
		*/
		function update_cate_data($data,$info = array()){
			$this->db->where('id', (int) $data['id']);
			$old_project_type = $data['old_project_type'];
			unset($data['old_project_type']);
 			$result = $this->db->update($this->cate_table, $data);
 			if($result){
 				/*$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'ad_cate'),'advertising_redis_model');
 				if($old_project_type != $data['project_type']){
 					$this->advertising_redis_model->redis_class->del_ad_cate($old_project_type, $data['sign']);
 				}
				$data = array_merge_recursive($data, $info);
				$this->advertising_redis_model->redis_class->save_ad_cate($data['sign'], $data);*/
				show_message($_SERVER['HTTP_REFERER'], "广告分类修改成功");
 			}else{
				show_message($_SERVER['HTTP_REFERER'], "广告分类修改失败,请重试！");
 			}
		}

		/**
		* 函数功能介绍：更新广告位缓存
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-15-02 15:04
		* 最后修改人：
		* 最后修改时间：
		*/
		function update_cate_cache($sign){
			$this->load->model('advertisings');
			$ad_list = $this->advertisings->get_ad_by_sign($sign);
			if($ad_list){
				$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
				foreach ($ad_list as $key => $value) {
					if(!empty($value['ad_image_url']) && !strstr($value['ad_image_url'], 'http://') ){
						$value['ad_image_url'] = get_qiniu_url($value['ad_image_url']);
					}else {
						$value['ad_image_url'] = $value['ad_image_url'];
					}
					$info = array();
					if($value['ad_type'] == 2 || $value['ad_type'] == 4) {//用户
						$info = get_field_by_where('user', 'id AS uid,sex,nick,head_image_url', 'id=' . $value['top_id']);
					}elseif ($value['ad_type'] == 3) {//视频
						$info = get_field_by_where('video AS v LEFT JOIN mi_user AS u ON (v.uid = u.id)', 'v.id AS vid,v.uid,u.sex,u.nick,u.head_image_url', 'v.id=' . $value['top_id']);
					}
					$data = array_merge($value, $info);
					$this->advertising_redis_model->redis_class->save_advertising($sign, $data);
				}
				show_message($_SERVER['HTTP_REFERER'], "更新成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "该广告位暂无广告");
			}
		}

		/**
		* 函数功能介绍：修改广告位排序
		* 创建人: 	Zhou Hailong
		* 创建时间: 2016-01-22 16:54
		* 最后修改人：
		* 最后修改时间：
		*/
		function update_cate_sorts() {
			$post_data = $this->input->post();
			$this->load->model('advertisings');
			$sort = $this->input->post('sort');
			if(is_array($sort)){
				$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'ad_cate'),'advertising_redis_model');
				foreach($sort as $key=>$val) {
					$this->advertisings->update_cate($key,array('sort'=>$val));
					$this->advertising_redis_model->redis_class->change_cate_sort($post_data['cat_name'][$key], $val);
				}
			}
			show_message($_SERVER['HTTP_REFERER'], "广告排序修改成功");
		}


		
		
		function view($id){
			if((int)$id > 0){
				$this->load->model('advertisings');
				$info = get_field_by_where($this->table, '*', 'id=' . $id);
				$cate_info = $this->advertisings->get_cate_list($info['cid']);
				$this->smarty->assign("cate_info", $cate_info);
				$this->smarty->assign("action", '查看广告详情');
				$this->smarty->assign("info", $info);
			}
			$this->smarty->display("news/view.html");
		}

		function view_cate($id){
			if((int)$id > 0){
				$info = get_field_by_where($this->cate_table, '*', 'id=' . $id);
				$this->smarty->assign("action", '查看广告详情');
				$this->smarty->assign("info", $info);
			}
			$this->smarty->display("news/view_cate.html");
		}

		/**
		* 函数功能介绍：删除广告
		* 创建人: 	Huang YongJun
		* 创建时间: 2015-03-05 18:04
		* 最后修改人：
		* 最后修改时间：
		* @param ids int
		*/
		function delete($sign = 0,$id = 0){
			if($_POST){
				$this->load->model('advertisings');
				$ids = $this->input->post('ids');
				if(count($ids) > 0){
					$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
					foreach ($ids as $key => $value) {
						$this->advertising_redis_model->redis_class->del_ad($_POST['sign'], $value);
					}
					$this->advertisings->batch_delete($ids);
					show_message($_SERVER['HTTP_REFERER'], "广告删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "您没有选中任何数据");exit;
				}
			}elseif( (int) $id > 0){
				
				$this->db->where('id', $id);
				$result = $this->db->delete($this->table);
				if($this->db->affected_rows() > 0){
					$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'advertising'),'advertising_redis_model');
					$this->advertising_redis_model->redis_class->del_ad($sign, $id);
					show_message($_SERVER['HTTP_REFERER'], "广告删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "广告删除失败，请重试！");
				}
			}
		}

		function delete_cate($id = 0){
			if($_POST){
				$this->load->model('advertisings');
				$ids = $this->input->post('ids');
				if(count($ids) > 0){
					/*$ad_info = $this->advertisings->get_ad_cate_info($ids);
					if($ad_info){
						$this->load->library('redis',array('model'=>'advertising_redis','channel'=>'ad_cate'),'advertising_redis_model');
						foreach ($ad_info as $key => $value) {
							$this->advertising_redis_model->redis_class->del_ad_cate($value['project_type'], $value['sign']);
						}
					}*/
					$this->advertisings->batch_cate_delete($ids);
					show_message($_SERVER['HTTP_REFERER'], "广告分类删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "您没有选中任何数据");exit;
				}
			}elseif( (int) $id > 0){
				$this->db->where('cid', $id);
				$this->db->delete($this->table);
				$this->db->where('id', $id);
				$result = $this->db->delete($this->cate_table);
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER'], "广告分类删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "广告分类删除失败，请重试！");
				}
			}
		}

		//获取二级分类
		function ignore_get_cate_by_pid($pid){
			$this->load->model('advertisings');
			if($pid > 0){
				$result = $this->advertisings->get_cate_id($pid);
			}
			if(is_ajax_request()){
				exit(json_encode(array("status" => 1, "result" => $result)));
			}else{
				return $result;
			}
		}
	}